<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <input type="text" class="uname" /><span>输入用户名</span>
    <script>
      let reg = /^[a-zA-Z0-9_-]{6,16}$/
      let uname = document.querySelector('.uname')
      let span = document.querySelector('span')
      uname.onblur = function () {
        if (reg.test(this.value)) {
          console.log('正确的')
          span.className = 'right'
          span.innerHTML = '用户名格式输入正确'
        } else {
          console.log('错误的')
          span.className = 'wrong'
          span.innerHTML = '用户名格式输入不正确'
        }
      }
    </script>

  </body>
</html>

